﻿using System.Collections.Generic;

namespace MathService.Values
{
    public class StatisticValue : IResetable
    {
        private readonly AverageValue f_Average;
        private readonly AverageValue f_Average2;
        private readonly MinMaxValue f_MinMax;

        public double M { get { return f_Average.Value; } }
        public double D
        {
            get
            {
                var m = M;
                return f_Average2.Value - m * m;
            }
        }

        public Interval MinMax {get { return f_MinMax; }}

        public int Count { get; private set; }

        public StatisticValue(int Length = 100)
        {
            f_Average = new AverageValue(Length);
            f_Average2 = new AverageValue(Length);
            f_MinMax = new MinMaxValue();
        }

        public void AddValue(double x)
        {
            f_Average.AddValue(x);
            f_Average2.AddValue(x * x);
            f_MinMax.AddValue(x);
            Count++;
        }

        public void AddEnumerable(IEnumerable<double> collection)
        {
            foreach(var value in collection)
                AddValue(value);
        }

        public void Reset()
        {
            f_Average.Reset();
            f_Average2.Reset();
            f_MinMax.Reset();
            Count = 0;
        }
    }
}